package com.tzg157.fitness.service.realtime.dwd;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.tzg157.fitness.service.realtime.HiveOnSparkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.spark.sql.SparkSession;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Slf4j
@Service("dwdMemberInfoService")
public class DWDMemberInfoServiceImpl implements HiveOnSparkService {

    @Override
    public void save(JSONObject jsonObject, SparkSession spark) {

        String username = (String) ((Map)jsonObject.get("data")).get("username");
        String gender = (String) ((Map)jsonObject.get("data")).get("gender");
        Integer age = (Integer) ((Map)jsonObject.get("data")).get("age");
        String timestamp_ = (String) jsonObject.get("timestamp_");
        //过滤掉空值数据、性别不符合的数据
        if(StrUtil.isBlank(username) || StrUtil.isBlank(gender) || age == null || StrUtil.isBlank(timestamp_)
                || !Arrays.asList("F","M").contains(gender)){
            log.warn("dwdMemberInfo filter:{}",jsonObject);
            return;
        }
//        spark.sql("use fitness");
        spark.sql("insert into fitness.ods_member_info values('"+username+"','"+gender+"',"+age+",'"+timestamp_+"')");
    }
}
